'use strict';

angular.module('app')
    .controller('bmBuildingController', ['$scope', '$rootScope', '$state', '$timeout', 'toaster',
        function($scope, $rootScope, $state, $timeout, toaster) {
            $scope.title = '楼栋列表';
            $scope.param = {};
            $scope.loading = false;
            $scope.regionName = $state.params.regionName;
            $scope.param.regionId = $state.params.regionId;
            $scope.parentid = '';
  
            $scope.search = function() {
                $scope.loading = true;
                if ($scope.pageFlag != 1) { //未点击翻页
                    $scope.param.pageNum = 1; //将页码设置为1，即跳转回第一页
                } else { //点击翻页
                    $scope.pageFlag = 2; //将开关值设置为2
                }
                $.ajax({
                    type: 'PUT',
                    dataType: 'json',
                    contentType: 'application/json;charset=UTF-8',
                    url: '/dac/bm/building/list',
                    data: angular.toJson($scope.param)
                }).then(function(result) {
                    $scope.loading = false;
                    if (result.httpCode == 200) {
                        $scope.pageInfo = result;
                    } else {
                        $scope.msg = result.msg;
                    }
                    $scope.$apply();
                });
            }
            //从本地获取localstorage数据，对权限按钮做隐藏
            var personAuth = localStorage.getItem("PersonAuth"); //获取PersonAuth的值
            personAuth = personAuth.split(',');
            for (var i = 0; i < personAuth.length; i++) {
                if (personAuth[i] == 'bm.building.read') {
                    $scope.read = true;
                }
                if (personAuth[i] == 'bm.building.add') {
                    $scope.add = true;
                }
                if (personAuth[i] == 'bm.building.update') {
                    $scope.update = true;
                }
                if (personAuth[i] == 'bm.building.delete') {
                    $scope.dele = true;
                }
            }
            $scope.search();


            $scope.delAuth = function(cardNo) {

                $scope.delParam = {};
                $scope.delParam.cardNo = cardNo;
                $.ajax({
                    type: 'POST',
                    dataType: 'json',
                    contentType: 'application/json;charset=UTF-8',
                    url: '/doorSysManager/authManager/delAuth',
                    data: angular.toJson($scope.delParam)
                }).then(function(result) {
                    if (result.httpCode == 200) { //成功
                        toaster.clear('*');
                        toaster.pop('success', '', "保存成功");
                        $timeout(function() {
                            $state.go('main.doorSysManager.authManager.list', null, {
                                reload: true
                            });
                        }, 1000);
                    } else {
                        toaster.clear('*');
                        toaster.pop('error', '', result.msg);
                        $scope.isDisabled = false;
                    }
                });
            }

            // 翻页
            $scope.pagination = function(page) {
                $scope.param.pageNum = page;
                $scope.pageFlag = 1; //将开关值设置为1
                $scope.search();
            };

            //找到与账号关联的区域ID
            function regionIdFind(arr, regionId) {
                for (var i = 0; i < arr.length; i++) {
                    if (arr[i].regionid == regionId) {
                        $scope.parentid = arr[i].parentid;
                        $scope.regionName = arr[i].regionname;
                        return;
                    }
                    if (arr[i].children.length > 0) {
                        regionIdFind(arr[i].children, regionId);
                    }

                }
            }
            //找出他的所有父树根展开
            function findParentTree(arr1, parentid) {
                findAll(arr1);
                function findAll(arr) {
                    for (let i = 0; i < arr.length; i++) {
                        if (arr[i].regionid == parentid) {
                            arr[i].collapsed = true;
                            parentid = arr[i].parentid;
                            findAll(arr1);
                        }
                        if (arr[i].children.length > 0) {
                            findAll(arr[i].children);
                        }
                    }
                }

            }

            $scope.getTreeList = function() {
                $.ajax({
                    type: 'POST',
                    dataType: 'json',
                    contentType: 'application/json;charset=UTF-8',
                    url: '/dac/regionManage/region/tree',
                    data: angular.toJson($scope.param)
                }).then(function(result) {
                    if (result.httpCode == 200) { //成功
                        $scope.angularTreeList = [];
                        var regionId = JSON.parse(localStorage.getItem('userInfo')).deptId;
                        $scope.param.regionId = regionId;
                        regionIdFind(result.data, regionId);
                        findParentTree(result.data, $scope.parentid);
                        //查询树  
                        $scope.orgTypeList = result.data;
                        //创建根节点  
                        var root = {};
                        root.regionname = "区域管理";
                        root.level = 0;
                        root.sequence = 1;
                        root.parentid = -1;
                        root.children = result.data;
                        $scope.angularTreeList.push(root);
                        //                     $scope.treeOptions.data=$scope.angularTreeList;
                        //                     toaster.clear('*');
                        //                     toaster.pop('success', '', "加载成功");
                    } else {
                        toaster.clear('*');
                        toaster.pop('error', '', result.msg);
                        $scope.isDisabled = false;
                    }
                });

            }

            $scope.getTreeList();

            $scope.clickRegion = function(regionId, regionName) {
                $scope.regionName = regionName;
                $scope.param.regionId = regionId;
                $scope.search();
            }


            $scope.showMsg = function(msg) {
                alert(msg);
            }

            $scope.delItem = function(id, name) {

                if (confirm('确认要删除楼栋[' + name + ']吗')) {
                    $.ajax({
                        type: 'POST',
                        dataType: 'json',
                        contentType: 'application/json;charset=UTF-8',
                        url: '/dac/bm/building/del',
                        data: angular.toJson({ 'id': id })
                    }).then(function(result) {
                        if (result.httpCode == 200) {
                            $scope.search();
                        } else {
                            toaster.clear('*');
                            toaster.pop('error', '', result.msg);
                        }
                    });
                }
            }
        }
    ]);